
package server;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * This class provides the functionality to connects the webserver 
 * to the actual java files, which gets the seeds names and other 
 * related info to creates the actual seeds
 * @author Valerie
 */
@WebServlet(name = "SeedServlet", urlPatterns = {"/SeedServlet"})
public class SeedServlet extends HttpServlet {
        private Admin adm = new Admin();
        
        /**
         * Takes in parameters from the request page and, if valid, creates a new seed in the database.
         * If they are invalid, returns an error message in the response.
         * 
         * 
         * @param request servlet request
         * @param response servlet response
         * @throws ServletException if a servlet-specific error occurs
         * @throws IOException if an I/O error occurs
         */
        protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		           try{
        String cname = request.getParameter("cname");
        Cookie ckcname = new Cookie("cname", cname);
        response.addCookie(ckcname);
        String sname = request.getParameter("sciname");
        Cookie cksname = new Cookie("sname", sname);
        response.addCookie(cksname);
        String zone = request.getParameter("zoneregion");
        String reqs = request.getParameter("reqs");
        Cookie ckreqs = new Cookie("reqs", reqs);
        response.addCookie(ckreqs);
        String tips = request.getParameter("tips");
        Cookie cktips = new Cookie("tips", tips);
        response.addCookie(cktips);
        String harvestInfo = request.getParameter("harvest");
        Cookie ckharvest = new Cookie("harvestInfo", harvestInfo);
        response.addCookie(ckharvest);
        String startGrow = request.getParameter("start");
        String stopGrow = request.getParameter("end");
        if(cname==null || cname.trim().isEmpty() || !cname.matches("\\p{Alnum}+")){
            response.sendRedirect("newseed.jsp?msg=cname");
        } else if (sname==null || sname.trim().isEmpty() || !sname.matches("\\p{Alnum}+")){
            response.sendRedirect("newseed.jsp?msg=sname");
        } else if (zone==null || zone.trim().isEmpty()  ){
            response.sendRedirect("newseed.jsp?msg=zone");
        } else if(startGrow==null || startGrow.trim().isEmpty() || !startGrow.matches("\\p{Alnum}+")){
            response.sendRedirect("newseed.jsp?msg=start");
        } else if(stopGrow==null || stopGrow.trim().isEmpty() || !stopGrow.matches("\\p{Alnum}+")){
            response.sendRedirect("newseed.jsp?msg=end");
        }
        else{
            if (reqs==null || reqs.trim().isEmpty()){
            reqs= "";
            }
            if(tips==null || tips.trim().isEmpty()){
                tips="";
            }
            if(harvestInfo==null || harvestInfo.trim().isEmpty()){
                harvestInfo="";
            }
            Cookie[] res = null;
            if(request!= null)
            res = request.getCookies();
            Cookie cur = null;
            if(res != null){
            for(int i = 0;i<res.length;i++){
            	if(res[i] != null)
            	if(res[i].getName().equals("user")){
            		cur = res[i];
            	}
            	
            }}
            // THIS IS SUPER SUPER TEMOPORARY PLEASE FIX ME SO I LOAD THE USER'S NAME PLZ
            if(cur != null){
            if(adm.makeSeed(cname, sname, Integer.parseInt(zone), reqs, tips, harvestInfo, startGrow, stopGrow, cur.getValue())){
                ckcname.setMaxAge(0);
                response.addCookie(ckcname);
                cksname.setMaxAge(0);
                response.addCookie(cksname);
                ckreqs.setMaxAge(0);
                response.addCookie(ckreqs);
                cktips.setMaxAge(0);
                response.addCookie(cktips);
                ckharvest.setMaxAge(0);
                response.addCookie(ckharvest);
                response.sendRedirect("newseed.jsp");
            }else{
                response.sendRedirect("newseed.jsp?msg=bad");
            }}
            else{
            	 response.sendRedirect("newseed.jsp?msg=bad");
            }
            }
        } finally{
                
            }
            

	}

	/**
	 * Does nothing.
         * 
         * @param request servlet request
         * @param response servlet response
         * @throws ServletException if a servlet-specific error occurs
         * @throws IOException if an I/O error occurs
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}
}